package com.example.redismanual.dao.actionExamples;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

// 限速器实现
@Component
public class Limiter {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    private ValueOperations<String, String> client;

    private String key = "limiter-key";

    @PostConstruct
    public void init(){
        this.client =  stringRedisTemplate.opsForValue();
    }

    public void set_max_execute_times(String max_execrute_times){
        this.client.set(this.key, max_execrute_times);
    }
   
    public Boolean still_valis(){
        return this.client.decrement(this.key) >= 0;
    }

    public int remaining_exe_times(){
        return Integer.parseInt(client.get(this.key));
    }
}
